package com.study.jdk8.stream;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

import java.util.List;


/**
 * @version V1.0
 * @title: SSHConnect
 * @description: ${DESCRIPTION}
 * @author: wanghuilong
 * @date: 2019-05-05 17:53
 * @copyright: http://fingo.com.my/
 */
@Slf4j
public class SSHConnect {
    private String hostName;
    private String userName;
    private String password;
    private int port;
    private File pemFile;
    Connection conn = null;

    /**
     * @param hostName
     * @param userName
     * @param password
     */
    public SSHConnect(String hostName, String userName, String password) {
        super();
        log.info("addLogStart SSHConnect InputParam: hostName : {} userName : {} password : {} ",
                hostName, userName, password);
        this.hostName = hostName;
        this.userName = userName;
        this.password = password;
    }

    public boolean passConnect() throws Exception {
        log.info("addLogStart passConnect InputParam: ");
        conn = new Connection(hostName);
        conn.connect();

        boolean isAuthenticated = conn.authenticateWithPassword(userName,
                password);

        return isAuthenticated;
    }

    public boolean publicKeyConnect() {
        log.info("addLogStart publicKeyConnect InputParam: ");
        conn = new Connection(hostName);

        boolean isAuthenticated = false;

        try {
            conn.connect();
            isAuthenticated = conn.authenticateWithPublicKey(userName, pemFile,
                    password);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return isAuthenticated;
    }

    public List execCommand(String command) {
        log.info("addLogStart execCommand InputParam: command : {} ", command);

        try {
            //            if(!passConnect()){
            //                if(!publicKeyConnect()){
            //                    throw new Exception("连接配置有问题哟！");
            //                }
            //            }
            Session session = conn.openSession();
            session.execCommand(command);

            InputStream stdout = new StreamGobbler(session.getStdout());
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
            List list = null;
            String line;

            while ((line = br.readLine()) != null) {
                list.add(line);
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public String getHostName() {
        log.info("addLogStart getHostName InputParam: ");

        return hostName;
    }

    public void setHostName(String hostName) {
        log.info("addLogStart setHostName InputParam: hostName : {} ", hostName);
        this.hostName = hostName;
    }

    public String getUserName() {
        log.info("addLogStart getUserName InputParam: ");

        return userName;
    }

    public void setUserName(String userName) {
        log.info("addLogStart setUserName InputParam: userName : {} ", userName);
        this.userName = userName;
    }

    public String getPassword() {
        log.info("addLogStart getPassword InputParam: ");

        return password;
    }

    public void setPassword(String password) {
        log.info("addLogStart setPassword InputParam: password : {} ", password);
        this.password = password;
    }

    public int getPort() {
        log.info("addLogStart getPort InputParam: ");

        return port;
    }

    public void setPort(int port) {
        log.info("addLogStart setPort InputParam: port : {} ", port);
        this.port = port;
    }

    public File getPemFile() {
        log.info("addLogStart getPemFile InputParam: ");

        return pemFile;
    }

    public void setPemFile(File pemFile) {
        log.info("addLogStart setPemFile InputParam: pemFile : {} ", pemFile);
        this.pemFile = pemFile;
    }

    public Connection getConn() {
        log.info("addLogStart getConn InputParam: ");

        return conn;
    }

    public void setConn(Connection conn) {
        log.info("addLogStart setConn InputParam: conn : {} ", conn);
        this.conn = conn;
    }
}
